$(function(){
	var curPage = 1;
    var pageSize = 100;

	var changeTime = function(time) {
		var date = new Date(time);
		var year = date.getFullYear();
		var month = date.getMonth()+1;
		var day = date.getDate();
		var hour = date.getHours();
		var min = date.getMinutes();

		return year+'-'+month+'-'+day+' '+hour+':'+min;
	}

	var mes = {
		getList: function() {
			$.ajax({
			    url: wsCommon.host + '/api/memberComments/listMsg/drafts',
			    dataType: wsCommon.jsonType,
			    success: function(res) {
					if(res.code == 1) {
						if(res.result) {
							var data = res.data;
							$('[data-role="total"] i').html(data.pager.total);
							$('[data-role="unread"]').html(data.unreadCount);
							$('[data-role="caogao"]').html(data.pager.total);

							$('[data-role="shoujian"]').html(data.pager.total);

							var pager = res.data.pager;

							mes.initPaging(pager.pages, pager.total, res);
						} else {
							wsCommon.setCookie('go_back_link', window.location.href);
							window.location.href = './passport-login.html';
						}
					}
			    },
			    error: function() {
			        // console.log('接口返回失败');
			    }
			})
		},
		randerList: function(pageData) {
			var tpl = '';
			$.each(pageData, function(index, item) {
				tpl += '<tr class="handle" data-role="handle" data-id="'+item.commentId+'">\
						  <td class="check"><input type="checkbox"  class="x-check op-check-all" data-id="'+item.commentId+'" data-role="delinput"></td>\
						  <td class="adressee">'+item.toUname+'</td>\
						  <td class="title">'+item.title+'</td>\
						  <td class="time">'+changeTime(item.time)+'</td>\
						</tr>';
			})
			$('[data-role="result"]').html(tpl);
		},
		getPageDates: function() {
            var self = this;

            $.ajax({
                url: wsCommon.host + '/api/memberComments/listMsg/drafts',
                dataType: wsCommon.jsonType,
                data: {
					pageNum: curPage,
					pageSize: pageSize
				},
                success: function(res) {

                    // 同步页码
                    var totalPage = res.data.pager.pages;

                    var pageData = res.data.pager.list;
                    var pager = res.data.pager;
                    self.randerList(pageData);
                },
                error: function() {
                    // console.log('分页接口失败');
                }
            });
        },
		initPaging: function(totalPage, totalDataNum, res) {
            var self = this,
                ele = $('[data-role="dirPaging"]');
                if(totalDataNum > 0) {
                    ele.show();
                    var pagination = ele.pagination(totalDataNum, {
                        num_edge_entries: 1,
                        num_display_entries: 4,
                        callback: function(page_index) {
                            if(page_index == 0) {
                                var totalPage = res.data.pager.pages;

                                var pageData = res.data.pager.list;
                                var pager = res.data.pager;
                                mes.randerList(pageData);
                            } else {
                                curPage = page_index + 1;
                                mes.getPageDates();
                            }
                        },
                        items_per_page: pageSize,
                        prev_text: "&lt;&lt;上一页",
                        next_text: "下一页&gt;&gt;",
                        link_to: "javascript:;"
                    });

                }else {
                    ele.hide();
                    $('[data-role="result"]').html('<tr class="first"><td colspan="5" class="no-information">暂无消息</td></tr>')

                }
        },
		del: function(){
			$('[data-role="del"]').on('click', function() {
				var $input = $('[data-role="delinput"]:checked');
				var idList = '';
				var arr = [];
				$.each($input, function(index, item) {
					arr.push($(item).attr('data-id'));
				})
				idList = arr.join(',');
				if(idList != '' ) {
					$.ajax({
					    url: wsCommon.host + '/api/memberComments/deleteMsg/drafts',
					    dataType: wsCommon.jsonType,
						data: {
							commentIds: idList,
						},
					    success: function(res) {
							if(res.code == 1) {
								if(res.result) {
									iosOverlay({
										text: "删除成功!",
										duration: 1500,
										onbeforehide: function() {
											window.location.reload();
										}
									});
								} else {
									wsCommon.setCookie('go_back_link', window.location.href);
									window.location.href = './passport-login.html';
								}
							} else {
								iosOverlay({
									text: res.reason,
									duration: 1500
								});
							}
					    },
					    error: function() {
					        // console.log('接口返回失败');
					    }
					})
				} else {
					iosOverlay({
						text: "请选择要删除的项目!",
						duration: 1500
					});
				}
			});

			$('.action-check-all').on('click', function() {
				if($(this).prop('checked')) {
					$('[data-role="delinput"]').prop('checked', true);
				} else {
					$('[data-role="delinput"]').prop('checked', false);
				}
				// $('[data-role="delinput"]').prop('checked', true);
			})
		},
		handelJump: function() {
			$(document).on('click', '[data-role="handle"]', function() {
				window.location.href = './member-mes_sendedit.html?commentId=' + $(this).attr('data-id');
			})
		},
		init: function() {
			this.getList();
			this.del();
			this.handelJump();
		}
	}

	mes.init();
})
